﻿using Convertor.Interface;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using Wpf.CommonTools.SystemExtension;
using Wpf.CommonTools.StringExtension;

namespace Convertor.Plugin.Filter
{
    public class Plugin : PluginBase
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.Filter;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.Filter;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Text") + " - " + UILanguage.FindResource("Plugin_Filter_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_Filter_Desc"); }
        }






        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            if (string.IsNullOrEmpty(config.FilterText))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_OpenConfigForm"), LogTypes.Normal | LogTypes.HidePluginName); }
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                }
                else
                {
                    if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_CloseConfigForm"), LogTypes.Error | LogTypes.HidePluginName); }
                    content.StopConvert = true;
                    return content;
                }
            }

            if (string.IsNullOrEmpty(config.FilterText))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Filter_Info_InputContent_Null"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            string returnValue = filter(content);

            return new ConvertParamters(content.LastInputContent, returnValue);
        }


        private string filter(IConvertParamters content)
        {
            var input = content.OutputContent.ToString();
            Regex reg = null;


            string value = string.Empty;
            switch (config.ReturnType)
            {
                case ReturnTypes.OriginalText:
                    try
                    {
                        reg = new Regex(config.FilterText, config.RegexOption);
                    }
                    catch (Exception ex)
                    {
                        if (Log != null) { Log(this, ex.ToString(), LogTypes.Error | LogTypes.HidePluginName); }
                        return string.Empty;
                    }
                    if (reg.IsMatch(input))
                    {
                        value = input;
                        if (Log != null) { Log(this, UILanguage.TryFindResource("Plugin_Filter_Info_Result_Contain"), LogTypes.Normal | LogTypes.HidePluginName); }
                    }
                    else
                    {
                        if (Log != null) { Log(this, UILanguage.TryFindResource("Plugin_Filter_Info_Result_NotContain"), LogTypes.Normal | LogTypes.HidePluginName); }
                    }
                    break;
                case ReturnTypes.FilterText:
                    try
                    {
                        reg = new Regex(config.FilterText, config.RegexOption);
                    }
                    catch (Exception ex)
                    {
                        if (Log != null) { Log(this, ex.ToString(), LogTypes.Error | LogTypes.HidePluginName); }
                        return string.Empty;
                    }
                    StringBuilder sb = new StringBuilder();
                    MatchCollection mc = reg.Matches(input);
                    foreach (Match m in mc)
                    {
                        sb.AppendLine(m.Value);
                    }
                    value = sb.ToString().TrimEnd();
                    if (Log != null) { Log(this, string.Format(UILanguage.TryFindResource("Plugin_Filter_Info_Result_Filter"), mc.Count), LogTypes.Normal | LogTypes.HidePluginName); }
                    break;
                default:
                case ReturnTypes.Replace:
                    try
                    {
                        reg = new Regex(config.FilterText, config.RegexOption);
                    }
                    catch (Exception ex)
                    {
                        if (Log != null) { Log(this, ex.ToString(), LogTypes.Error | LogTypes.HidePluginName); }
                        return string.Empty;
                    }
                    switch (config.ReplaceType)
                    {
                        default:
                        case ReplaceTypes.CustomText:
                            value = reg.Replace(input, config.ReplaceText);
                            break;
                        case ReplaceTypes.InputContent:
                            value = reg.Replace(input, content.LastInputContent.ToString());
                            break;
                        case ReplaceTypes.ToLower:
                            value = reg.Replace(input, m => m.ToString().ToLower());
                            break;
                        case ReplaceTypes.ToUpper:
                            value = reg.Replace(input, m => m.ToString().ToUpper());
                            break;
                    }
                    if (Log != null) { Log(this, string.Format(UILanguage.TryFindResource("Plugin_Filter_Info_Result_Replace"), config.ReplaceText), LogTypes.Normal | LogTypes.HidePluginName); }
                    break;
                case ReturnTypes.StringFormat:
                    try
                    {
                        value = string.Format(config.FilterText, input);
                    }
                    catch { }
                    break;
            }

            return value;
        }

        private void toUpLower()
        {}

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
